package com.tanhua.management.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.management.mapper.LogMapper;
import com.tanhua.management.vo.LogVo;
import com.tanhua.management.vo.PageResult;
import com.tanhua.management.pojo.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoggerService {


    @Autowired
    private LogMapper logMapper;


    public PageResult logList(Long uid, Integer pageSize, Integer page, String sortProp, String sortOrder) {

        PageResult pageResult = new PageResult();

        //查数据
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
//        if (StringUtils.equals("ascending ", sortOrder)) {
//            queryWrapper.eq("uid", uid).orderByAsc(sortProp);
//        } else {
//            queryWrapper.eq("uid", uid).orderByDesc(sortProp);
//        }
        List<Log> logsList = logMapper.selectList(queryWrapper);
        List<LogVo> logVoList = new ArrayList<>();

        for (Log log : logsList) {
            LogVo logVo = new LogVo();
            logVo.setId(log.getId());
            logVo.setLogTime(log.getLogTime().getTime());
            logVo.setPlace(log.getPlace());
            logVo.setEquipment(log.getEquipment());
            logVoList.add(logVo);
        }
        if (StringUtils.equals("descending", sortOrder)) {
            logVoList.sort((o1,o2) -> (int) (o2.getLogTime() - o1.getLogTime()));
        } else {
            logVoList.sort((o1,o2) -> (int) (o1.getLogTime() - o2.getLogTime()));
        }
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setPages(0);
        pageResult.setCounts(logVoList.size());
        pageResult.setItems(logVoList);

        return pageResult;
    }
}
